package com.pl.itsense.gwt.clientzone.ui;


import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.pl.itsense.gwt.clientzone.client.ClientZoneServiceAsync;
import com.pl.itsense.gwt.clientzone.client.Clientzone;
import com.pl.itsense.gwt.clientzone.model.MemberDTO;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler; 
import com.smartgwt.client.types.MultipleAppearance;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.form.fields.LinkItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.validator.MatchesFieldValidator;
import com.smartgwt.client.widgets.form.validator.RegExpValidator;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class ChangePreferencjePanel extends Window{
	static ChangePreferencjePanel instance = null;
	DynamicForm form = new DynamicForm();

	SelectItem selectItemIntrests = new SelectItem();  
	CheckboxItem newsLatterItem = new CheckboxItem();
	TextAreaItem descriptionItem = new TextAreaItem();  


	IButton okButton = new IButton("Zmień dane");
	IButton cancelButton = new IButton("Anuluj");

	public ChangePreferencjePanel() {
		instance = this;
		init();
	}
	
	
	private void init(){
		setWidth(460);  
		setHeight(280);
		setShowCloseButton(false);
		setTitle("Zmiana ustawień klienta - ITSense Strefa Klienta");  
		setShowMinimizeButton(false);
		setIsModal(true);  
		setShowModalMask(true);  
		centerInPage();  
		setCanDragReposition(false);

		descriptionItem.setName("descriptionItem");  
		descriptionItem.setTitle("Komentarz");  
		descriptionItem.setWidth(250);
		descriptionItem.setValue(Clientzone.memberDTO.getComment());
		
		
		
		newsLatterItem.setName("newsLatterTerms");  
		newsLatterItem.setTitle("Newsletter");  
		newsLatterItem.setWrapTitle(true);
		newsLatterItem.setRequired(false);
		newsLatterItem.setValue(Clientzone.memberDTO.getNewsletter());
		
		
		selectItemIntrests.setName("intrests");
		selectItemIntrests.setTitle("Zainteresowania");  
		selectItemIntrests.setMultiple(true);
		selectItemIntrests.setWidth(250);
		selectItemIntrests.setMultipleAppearance(MultipleAppearance.PICKLIST);  
		selectItemIntrests.setValueMap("Bazy danych", "Hurtownie danych","Systemy raportowe","Systemy analityczne","Business intelligence","Intranet","Obsluga informatyczne","Modelowanie procesów biznesowych");
		selectItemIntrests.setValue(Clientzone.memberDTO.getIntrests());
		
		
		
		
		form.setWidth(300);
		form.setFields(selectItemIntrests, descriptionItem, newsLatterItem);
	
		okButton.addClickHandler(new ClickHandler() {  
			public void onClick(ClickEvent event) {
				if (form.validate()){
					MemberDTO memberDTO = Clientzone.memberDTO;
					memberDTO.setIntrests(form.getValueAsString("intrests"));
					memberDTO.setNewsletter( (Boolean)form.getValue("newsLatterTerms"));
					memberDTO.setComment(form.getValueAsString("descriptionItem"));

					Clientzone.clientZoneService.update(memberDTO, new AsyncCallback<MemberDTO>() {
						@Override
						public void onSuccess(MemberDTO result) {
							SC.say("Twoje dane kontaktowe zostały pomyślnie zmienione.", new BooleanCallback() {
								@Override
								public void execute(Boolean value) {
									instance.destroy();
									instance = null;
								}
							});
						}
						
						@Override
						public void onFailure(Throwable caught) {
							SC.say("Wystąpił błąd podczas modyfikacji danych kontaktowych", new BooleanCallback() {
								
								@Override
								public void execute(Boolean value) {
									instance.destroy();
									instance = null;
								}
							});
						}
					});

					
					
					
				}
				instance.destroy();
				instance = null;
			}
		});
		
		cancelButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				instance.destroy();
				instance = null;
			}
		});

		form.setTop(20);
		form.setLeft(40);
		
		VLayout vLayout = new VLayout();
		vLayout.addChild(form);
		
		
		HLayout hLayout = new HLayout();
		hLayout.setHeight(30);
		hLayout.addMember(okButton);
		hLayout.addMember(cancelButton);		
		hLayout.setTop(190);
		hLayout.setLeft(132);
		hLayout.setMembersMargin(5);  
		vLayout.addChild(hLayout);
		
		addItem(vLayout);
		
	
	}

}
